﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class CharAnimLoop
{
	public string name = "New Anim Loop";
	public List<Texture> frames;
}

public class CharAnim : MonoBehaviour
{
	public int currentLoop = 0;
	public int currentFrame = 0;

	public float frameTime = 0.3f;
	private float nextFrameSchedule;

	public List<CharAnimLoop> animationLoops;

	void Update()
	{
		PlayingAnim();
	}

	void PlayingAnim()
	{
		if (Time.time < nextFrameSchedule)
			return;

		int lastFrame = currentFrame;

		CharAnimLoop loop = animationLoops[currentLoop];

		currentFrame++;

		if (currentFrame > loop.frames.Count)
			currentFrame = 0;

		if (currentFrame != lastFrame)
			SetFrame(currentFrame);

		ScheduleNextFrame();
	}

	void SetFrame(int frameid)
	{
		renderer.sharedMaterial.mainTexture = animationLoops[currentLoop].frames[frameid];
	}

	void ScheduleNextFrame()
	{
		nextFrameSchedule = Time.time + frameTime;
	}
}
